# !/usr/bin/env python
# -*- coding:utf-8 -*-
import re

Template = """
    <div   v-for="(item , index122  ) in list['1']['key34'].list" v-bind:key="item.id" >
        <span>{{item.id}}</span>
    </div>
    <div v-for="item1   in list" :key="item11.ids1">
        <span>{{item1.id}}</span>
    </div>
    <div v-if="true&&false||4<=8">不显示</div>
    <div v-else-if="false">还是不显示</div>
    <div v-else-if="(4/2-1)*3%5">还是不显示</div>
    <div v-else>最后才显示</div>

    <div v-show="true">这个是要显示的</div>
    <div v-show="false">这个是要显示的</div>
    <div v-show="item ">这个是要显示的</div>
    <div v-show="item == 1">这个是要显示的</div>
    <div v-show="item == 1 && false">这个是要显示的</div>
    <div v-show="item-1">这个是要显示的</div>

    <div :clakeyss="['test',test+&&'atctve"]" :style=' "background-color:"+(test?"test": "44")+ ' ></div>
    <div v-bind:class="['test',test&&'atctve']"></div>
    <img :src="'dd'" />


    <input type="test" v-model=" test_var['es'] " />
"""


class Instruct():
    test1 = 123
    executeErgodic = True
    executeJudge = True
    executeShow = True
    executeModel = True
    executeDirectives = True

    '''
    vue指令转小程序属性
    '''
    def __init__(self,template, Ergodic = executeErgodic,Judge = executeJudge,show = executeShow,model = executeModel,directives = executeDirectives ):
        """
        初始化
        """
        self.RenderLayer = []
        # 记录渲染层
        self.template =  template
        self.executeErgodic = Ergodic
        self.executeJudge = Judge
        self.executeShow = show
        self.executeModel = model
        self.executeDirectives = directives

    def run(self):
        '''
        执行入口方法
        '''
        self.executeErgodic and self.ergodic()
        # 执行转换v-for 
        self.executeJudge and self.judge()
        # 执行转换 v-if
        self.executeShow and self.showChange()
        # 执行转换 v-model
        self.executeModel and self.modelChange()
        self.executeDirectives and self.directivesChange()
        print(self.template)

    def ergodic(self,template = None):
        '''
        遍历转换
        v-for =>wx:for
        :key => wx:key
        '''
        template =template or self.template
        # print(template)
        # key转化函数
        def handleFor(matched):
            match_txt = matched.group()
            # print('matched:'+match_txt)
            if ',' in match_txt :
                itemName =re.search(r'(?<=(\'|\")[(]{1})\w+',match_txt).group()
                indexName =re.search(r'\w+(?=\s*[)])',match_txt).group()
                
            else:
                itemName =re.search(r'(?<=(\'|\"))\w+',match_txt).group()
                indexName = 'index'
            
            listName =re.search(r'(?<=in\s).*[()\[\]]*(?=\'|\")',match_txt).group()
            # print(listName, itemName, indexName)
            return 'wx:for="{{%s}}" wx:for-item="%s" wx:for-index="%s"' % (listName, itemName, indexName)
        
        template = re.sub(r"v-for=(\'|\")[()\w\s,]*(\sin\s){1,}[\w\[\].\'\"]*\1", handleFor, template)

        # key转化函数
        # def handleKey(matched):

        #     keyName = re.search(r'(?<=\.)\w+',matched.group()).group()
        #     # return str(value * 2)
        #     return 'wx:key="%s"' % keyName
        
        template = re.sub(r"(v-bind)?:key=(\'|\")\w+\.+\w+\2", (lambda matched: 'wx:key="%s"' % re.search(r'(?<=\.)\w+',matched.group()).group() ), template)
        self.template = template
        # 实例内部使用
        # print(template,'5555')
        return template
        # 单独使用
                

    def judge(self,template=None):
        """
        判断转换
        v-if => wx:if
        """
        template =template or self.template
        
        
        def handleJudge(matched):
            match_txt = matched.group()
            # print('matched:'+match_txt)
            # print('3333333333')
            judgeName = 'wx:if'
            if 'v-else-if' in match_txt :
                judgeName = 'wx:elif'
            
            
            boolNmae =re.search(r'(?<=\'|\").*(?=\'|\")',match_txt).group()
            # print(judgeName)
            # print(boolNmae)
            return '%s="{{%s}}"' % (judgeName, boolNmae)
        
        # 匹配v-else
        template = re.sub(r"v-(else-)?if=(\'|\").*\2", handleJudge, template)
        # 匹配v-else
        template = re.sub(r"v-else", 'wx:else', template)
        self.template = template
        # 实例内部使用
        return template
        # 单独使用


    def showChange(self,template=None):
        '''
        v-show => hidden
        '''
        template =template or self.template
        def showhandle(matched):
            match_txt = matched.group()
            boolvalue = re.search(r'(?<=\'|\").*(?=\'|\")',match_txt).group()
            # print(boolvalue)
            
            value = re.search(r'^(\s)*(\w)*(\s)*$',boolvalue)
            equal = re.search(r'^(\s)*(\w)*(\s)*={2,3}(\s)*(\w)*(\s)*$',boolvalue)
            # print(value)
            if boolvalue == 'true':
                boolvalue = 'false'
            elif boolvalue == 'false':
                boolvalue = 'true'
            elif value :
                boolvalue = '!' + value.group()
            elif equal :
                boolvalue  = re.sub(r"==", '!=', equal.group())
            else :
                boolvalue = '!(%s)' % boolvalue
            
            # print(boolvalue)
            # print('----------')
            return 'hidden="{{%s}}"' % boolvalue
        
        template = re.sub(r"v-show=(\'|\").*\1", showhandle, template)
        self.template = template
        # 实例内部使用
        return template
        # 单独使用

    def modelChange(self,template=None):
        '''
        v-model => 
        '''

        template =template or self.template
        def modelhandle(matched):
            match_txt = matched.group()
            valuename = re.sub(r'\s', '',match_txt)
            valuename = re.search(r'(?<=\'|\").*(?=\'|\")',valuename).group()
            return 'value="{{%s}}" data-key="%s"  bindinput="write" ' % (valuename,valuename)
        
        template = re.sub(r"v-model=(\'|\")\s*[\w.\[\]()\'\"]*\s*\1", modelhandle, template)
        self.template = template
        # 实例内部使用
        return template
        # 单独使用


    def directivesChange(self,template=None):
        '''
        修改动态变量
        '''
        template =template or self.template
        def handle(matched):
            match_txt = matched.group()
            print(match_txt)
            keyName = re.search(r'(?<=:).*(?==)',match_txt).group()
            valueName = re.search(r'(?<=\'|\")\w*(?=\'|\")',match_txt).group()


            return '%s="{{%s}}"' % (keyName,valueName)
        
        template = re.sub(r"(v-bind)?:((?<!key)\[{0,1}\w*\]{0,1})=(\'|\")([\w.\[\]()\'\"&|,\s\+\-\?:;](?<!\3))*\3", handle, template)
        self.template = template
        # 实例内部使用
        # print(template,'5555')
        return template
        # 单独使用

    def test(self):
        '''
        测试
        '''
        # print(re.search(r"v-for=(\'|\") \(* \w+,*\b \)* in\b\w(\'|\")", self.template))
        # print(re.search(r"v-for=\"\[()\w\s,]+\bin\b\w+\"", self.template))
        # print(re.sub(r"\s", '',  ' e  e   e      '))
        print(re.search(r'ee(\'|\")(.(?<!\1))*\1',"""ee'df'dfdf' """).group())


if __name__ == "__main__":
    
    instruct = Instruct(Template,)
    # print(Instruct.executeErgodic)
    # print(instruct.executeErgodic)
    instruct.run()
    # instruct.test()
